package pl.edu.mimuw.irs.core.model.analysis.dd;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import pl.edu.mimuw.irs.core.model.design.IRSActivity;
import pl.edu.mimuw.irs.core.model.design.processing.Requirement;

@Deprecated
public class Cycle {

	// *** Attributes

	/* According to constructor note - dependencies are stored in reverse order. */
	private LinkedHashMap<Requirement, Dependency> dependencies;
	/*
	 * Involved activities are these activities that caused this cycle.
	 */
	private Set<ActivityWrapper> involvedActivities;
	public LinkedHashMap<Requirement, Dependency> getDependencies() {
		if (dependencies == null) dependencies = new LinkedHashMap<Requirement, Dependency>();
		return dependencies;
	}
	public Set<ActivityWrapper> getInvolvedActivities() {
		if (involvedActivities == null) involvedActivities = new LinkedHashSet<ActivityWrapper>();
		return involvedActivities;
	}
	
	// *** Constructors
	
	/* Dependencies cycle passed to constructors must be in reverse order to the
	 * original direction (A <- B <- C <- A should be passed as [A, B, C]).
	 */
	@SuppressWarnings("unchecked")
	public Cycle(LinkedHashMap<Requirement, Dependency> dependencies) {
		LinkedHashMap<Requirement, Dependency> clone = (LinkedHashMap<Requirement, Dependency>) dependencies.clone();
		this.dependencies = clone;
	}
	
	public Cycle(LinkedHashSet<Dependency> dependencies) {
		LinkedHashMap<Requirement, Dependency> map = getDependencies();
		for (Dependency dependency : dependencies) {
			map.put(dependency.getRequirement(), dependency);
		}
	}
	
	public Cycle(List<Dependency> dependencies) {
		LinkedHashMap<Requirement, Dependency> map = getDependencies();
		for (Dependency dependency : dependencies) {
			map.put(dependency.getRequirement(), dependency);
		}
	}

	public Cycle(LinkedHashSet<Dependency> path, Dependency dependency) {
		/* This constructor knows that path can be longer than the actual 
		 * cycle. This means the dependency parameter is where the cycle
		 * starts and ends. 
		 */
		LinkedHashMap<Requirement, Dependency> map = getDependencies();
		for (Dependency node : path) {
			if (map.size() == 0 && dependency == node) {
				map.put(node.getRequirement(), node);
			} else if (map.size() > 0) {
				map.put(node.getRequirement(), node);
			}
		}
	}
	
	// *** Methods
	
	@Override
	public String toString() {
		return Cycle.class.getSimpleName() + getDependencies();
	}

	public boolean contains(Dependency dependency) {
		return getDependencies().containsValue(dependency);
	}

	public boolean isDeadlocking(IRSActivity activity) {
		/*
		 * We search for all descendants of given activity in this cycle. 
		 */
		return true;
	}
	
}
